﻿// COPYRIGHT (C) Tom. ALL RIGHTS RESERVED.
// THE AntdUI PROJECT IS AN WINFORM LIBRARY LICENSED UNDER THE Apache-2.0 License.
// LICENSED UNDER THE Apache License, VERSION 2.0 (THE "License")
// YOU MAY NOT USE THIS FILE EXCEPT IN COMPLIANCE WITH THE License.
// YOU MAY OBTAIN A COPY OF THE LICENSE AT
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE
// DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
// SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING PERMISSIONS AND
// LIMITATIONS UNDER THE License.
// GITCODE: https://gitcode.com/AntdUI/AntdUI
// GITEE: https://gitee.com/AntdUI/AntdUI
// GITHUB: https://github.com/AntdUI/AntdUI
// CSDN: https://blog.csdn.net/v_132
// QQ: 17379620

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dictionary
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;

    public class IniParser
    {
        public Dictionary<string, Dictionary<string, string>> Parse(string filePath)
        {
            var iniData = new Dictionary<string, Dictionary<string, string>>();
            string currentSection = "Global";

            if (!File.Exists(filePath))
                throw new FileNotFoundException("INI文件不存在", filePath);

            string[] lines = File.ReadAllLines(filePath);

            foreach (string line in lines)
            {
                string trimmedLine = line.Trim();

                if (IsCommentOrEmpty(trimmedLine))
                    continue;

                if (trimmedLine.StartsWith("[") && trimmedLine.EndsWith("]"))
                {
                    currentSection = trimmedLine.Substring(1, trimmedLine.Length - 2);
                    if (!iniData.ContainsKey(currentSection))
                        iniData[currentSection] = new Dictionary<string, string>();
                }
                else
                {
                    // 使用新的解析方法
                    string key;
                    string value;
                    if (TryParseKeyValue(trimmedLine, out key, out value))
                    {
                        if (!iniData.ContainsKey(currentSection))
                            iniData[currentSection] = new Dictionary<string, string>();

                        iniData[currentSection][key] = value;
                    }
                }
            }

            return iniData;
        }

        // 使用 out 参数替代元组
        private bool TryParseKeyValue(string line, out string key, out string value)
        {
            key = null;
            value = null;

            int equalsIndex = line.IndexOf('=');
            if (equalsIndex <= 0) return false;

            key = line.Substring(0, equalsIndex).Trim();
            value = line.Substring(equalsIndex + 1).Trim();

            // 处理值中的注释
            int commentIndex = value.IndexOf(';');
            if (commentIndex >= 0)
                value = value.Substring(0, commentIndex).Trim();

            return true;
        }

        private bool IsCommentOrEmpty(string line)
        {
            return string.IsNullOrWhiteSpace(line) ||
                   line.StartsWith(";") ||
                   line.StartsWith("#") ||
                   line.StartsWith("//");
        }

        public void PrintAllValues(Dictionary<string, Dictionary<string, string>> iniData)
        {
            foreach (var section in iniData)
            {
                Console.WriteLine($"[{section.Key}]");
                foreach (var keyValue in section.Value)
                {
                    Console.WriteLine($"  {keyValue.Key} = {keyValue.Value}");
                }
                Console.WriteLine();
            }
        }
    }
}
